package org.qiyu.live.api.controller;

import com.alibaba.nacos.shaded.com.google.common.collect.Maps;
import org.apache.catalina.User;
import org.apache.dubbo.config.annotation.DubboReference;
import org.qiyu.live.user.dto.UserDTO;
import org.qiyu.live.user.interfaces.IUserRpc;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 作者：無言
 * 时间：2024/11/13 18:23
 * 描述：永无BUG
 */

@RestController
@RequestMapping("/user")
public class UserController {

//    @DubboReference(url = "dubbo://192.168.239.101:9090") 可以指定服务
    @DubboReference
    private IUserRpc userRpc;

    @RequestMapping("/getUserInfo")
    public UserDTO getUserInfo(Long userId) {
        return userRpc.getByUserId(userId);
    }

    @GetMapping("/updateUserInfo")
    public boolean updateUserInfo(Long userId, String nickName) {
        UserDTO userDTO = new UserDTO();
        userDTO.setUserId(userId);
        userDTO.setNickName(nickName);
        return userRpc.updateUserInfo(userDTO);
    }

    @GetMapping("/insertOne")
    public boolean insertOne(Long userId) {
        UserDTO userDTO = new UserDTO();
        userDTO.setUserId(userId);
        userDTO.setNickName("test-1113");
        userDTO.setSex(1);
        return userRpc.insertOne(userDTO);
    }

    @GetMapping("/batchQueryUserInfo")
    public Map<Long, UserDTO> batchQueryUserInfo(String userIdStr) {

        List<Long> userIdList = Arrays.stream(userIdStr.split(","))
                                      .map(String::trim)
                                      .filter(s -> !s.isEmpty())
                                      .mapToLong(Long::valueOf)
                                      .boxed()
                                      .collect(Collectors.toList());

        return userRpc.batchQueryUserInfo(userIdList);

    }


}
